package com.le.tester.javaThink.fifth.chapter24_ConcurrentProgramming;

/**
 * createTime：2021/8/23 13:42
 * description：CompletableFuture 来响应异常
 */
public class CatchCompletableExceptions {
    static void handleException(int failCount) {
        CompletableExceptions.test("exceptionally", failCount)
                .exceptionally((ex) -> {
                    if (ex == null) {
                        System.out.println("I do not get it yet");
                    }
                    assert ex != null;
                    return new Breakable(ex.getMessage(), 0);
                }).thenAccept(str -> System.out.println("result:" + str));

        CompletableExceptions.test("handle", failCount)
                .handle((result, fail) -> {
                    if (fail != null) {
                        return "Failure recovery object";
                    } else {
                        return result + " is good";
                    }
                }).thenAccept(str -> System.out.println("result: " + str));

        CompletableExceptions.test("whenComplete", failCount)
                .whenComplete((result, fail) -> {
                    if (fail != null) {
                        System.out.println("it failed");
                    } else {
                        System.out.println(result + " OK");
                    }
                }).thenAccept(r -> System.out.println("result: " + r));

    }

    public static void main(String[] args) {
        System.out.println("**** Failure Mode ****");
        handleException(2);
        System.out.println("**** Success Mode ****");
        handleException(0);
    }
}
